﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using OnlineVideos.Common;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Threading;
using PicasaMobileInterface;
using Microsoft.Phone.Scheduler;


namespace OnlineVideos.Library
{
    public static class ShowDownloader
    {
        static DispatcherTimer downloadtimer = default(DispatcherTimer);
        public static void StartBackgroundDownload(DispatcherTimer timer = default(DispatcherTimer))
        {
            try
            {
                //Deployment.Current.Dispatcher.BeginInvoke(() =>
                //{
                //    if (downloadtimer != default(DispatcherTimer))
                //        downloadtimer = timer;

                //});
                //Wait for ten seconds before to start downloading shows.
                if (NetworkHelper.IsNetworkAvailableForDownloads())
                {
                    try
                    {
                        if (ValidationHelper.IsDownloadCompletedToday())
                        {
                            try
                            {
                                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                {
                                    if (timer != default(DispatcherTimer))
                                        timer.Stop();
                                  

                                });
                                PersonalizationDataSync personalization = new PersonalizationDataSync();
                                switch (AppSettings.BackgroundAgentStatus)
                                {

                                    case SyncAgentStatus.DownloadParentalControlPreferences:
                                        try
                                        {
                                            personalization.DownloadParentalPreferences();
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                    case SyncAgentStatus.UploadParentalControlPreferences:
                                        try
                                        {
                                            personalization.UploadParentalPreferences();
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                    case SyncAgentStatus.DownloadPeople:
                                        try
                                        {
                                            if (AppSettings.Downloadpeople)
                                                MovieUpdates.checkForPeopleUpdates();
                                            else
                                                AppSettings.BackgroundAgentStatus = SyncAgentStatus.DownloadShows;
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UpdatePeople:
                                        try
                                        {
                                            if (Task.Run(async () => await Storage.FileExists("Peoples.xml")).Result)
                                                MovieUpdates.GetPeopleCompleted("Peoples.xml");
                                            else
                                                AppSettings.BackgroundAgentStatus = SyncAgentStatus.DownloadShows;
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.DownloadShows:
                                        try
                                        {
                                            if (AppSettings.Downloadshows)
                                                MovieUpdates.checkForShowUpdates();
                                            else
                                                AppSettings.BackgroundAgentStatus = SyncAgentStatus.UpDateLiveTile;
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UpdateShows:
                                        try
                                        {
                                            if (Task.Run(async () => await Storage.FileExists("Movies.xml")).Result)
                                            {
                                                MovieUpdates.DownloadNextShowWithTopVideosAndAppCountCompleted("Movies.xml");
                                            }
                                            else
                                                AppSettings.BackgroundAgentStatus = SyncAgentStatus.UpDateLiveTile;
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UpDateLiveTile:
                                        try
                                        {
                                          
                                                Constants.LiveTileBackgroundAgentStatus = true;
                                                LiveTileUpDate.LiveTileForCycle();
                                          
                                        }
                                        catch(Exception ex)
                                        {
                                            string mg = ex.Message;
                                        }
                                        break;
                                    case SyncAgentStatus.UploadSearch:
                                        try
                                        {
                                            personalization.UploadSearch();
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UploadRatingForShows:
                                        try
                                        {
                                            BloggerInterface BI = new BloggerInterface(AppSettings.RatingUserName, AppSettings.RatingPassword);
                                            personalization.UploadRatingForShows();
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UploadRatingForVideos:
                                        try
                                        {
                                            BloggerInterface B1I = new BloggerInterface(AppSettings.RatingUserName, AppSettings.RatingPassword);
                                            personalization.UploadRatingForVideos();
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.DownloadRatingForShows:
                                        try
                                        {
                                            personalization.DownloadRatingForShows();
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.DownloadRatingForVideos:
                                        try
                                        {
                                            personalization.DownloadRatingForVideos();
                                           
                                        }
                                        catch
                                        {

                                        }
                                        break;
                                    case SyncAgentStatus.UpdateBlogUserNameandPassword:
                                         try
                                        {
                                        personalization.GetBlogDetails();
                                        AppSettings.StopTimer = "True";
                                        }
                                         catch
                                         {

                                         }
                                        break;
                                }
                                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                {
                                    if (timer != default(DispatcherTimer))
                                        timer.Start();

                                });
                            }
                            catch (Exception ex)
                            {
                                ex.Data.Add("Date", DateTime.Now);
                                Exceptions.SaveOrSendExceptions("Failed to download movie updates - {0}", ex);
                            }

                        }
                        else
                        {
                            AppSettings.StopTimer = "True";
                            if (SyncAgentState.SyncAgent != null)
                                ((IDowloadCompleteCallback)SyncAgentState.SyncAgent).OnStartEvent();
                        }

                    }
                    catch (Exception ex)
                    {
                        ex.Data.Add("Date", DateTime.Now);
                        Exceptions.SaveOrSendExceptions("Failed to download movie updates - {0}", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("Date", DateTime.Now);
                Exceptions.SaveOrSendExceptions("Failed to download movie updates - {0}", ex);
            }
            finally
            {
                if (SyncAgentState.SyncAgent != null && AppSettings.AppStatus != ApplicationStatus.Launching && AppSettings.AppStatus != ApplicationStatus.Active)
                {

                    ((IDowloadCompleteCallback)SyncAgentState.SyncAgent).OnStartEvent();
                }
            }
        }
    }
}

